package com.lemon.smartsafety.utils;

import android.content.Context;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiDetailSearchOption;
import com.baidu.mapapi.search.poi.PoiIndoorResult;
import com.baidu.mapapi.search.poi.PoiNearbySearchOption;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.poi.PoiSortType;
import com.lemon.smartsafety.utils.app.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/18 11:00
 * 修改人：Fargo
 * 修改时间：2016/8/18 11:00
 * 修改备注：   兴趣点检索
 */
public class PoiNearbySearchUtil implements
        OnGetPoiSearchResultListener {


    private Context context;

    public PoiNearbySearchUtil(Context context) {
        this.context = context;
    }
    private PoiSearch mPoiSearch = null;



    //POI  基本检索返回结果
    private PoiResult poiResult=null;

    private CallBackInterface mCallBackInterface=null;
    //页码
    private     int loadIndex = 0;
    //搜索半径
    private     int radius = 2000;
    //搜索每页条数
    private     int capacity = 10;

    //POI 详情检索
    private PoiDetailResult poiDetailResult=null;

    private CallBackInterface mDetailCallBack=null;





    public CallBackInterface getmCallBackInterface() {
        return mCallBackInterface;
    }

    public void setmCallBackInterface(CallBackInterface mCallBackInterface) {
        this.mCallBackInterface = mCallBackInterface;
    }

    public interface CallBackInterface {
        public void callBackFunction(boolean isSuccess);

    }


    public PoiDetailResult getPoiDetailResult() {
        return poiDetailResult;
    }

    public void setPoiDetailResult(PoiDetailResult poiDetailResult) {
        this.poiDetailResult = poiDetailResult;
    }

    public PoiResult getPoiResult() {
        return poiResult;
    }

    public void setPoiResult(PoiResult poiResult) {
        this.poiResult = poiResult;
    }


    public int getLoadIndex() {
        return loadIndex;
    }

    public void setLoadIndex(int loadIndex) {
        this.loadIndex = loadIndex;
    }

    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    /**
     * 响应周边搜索事件
     *
     * @param latLng
     */
    public void searchNearbyProcess(LatLng latLng,String key,CallBackInterface callBackInterface) {
        if(CommonUtil.strIsEmpty(key)){
            return;
        }
        mCallBackInterface=callBackInterface;

        mPoiSearch = PoiSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);


        PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption().keyword(key).sortType(PoiSortType.distance_from_near_to_far).location(latLng)
                .radius(radius).pageNum(loadIndex).pageCapacity(capacity);
        mPoiSearch.searchNearby(nearbySearchOption);
    }


    /**
     * 搜索POI详情
     *
     * @param uid
     */
    public void searchPoiDetail(String uid,CallBackInterface callBackInterface){
        mDetailCallBack=callBackInterface;

        mPoiSearch.searchPoiDetail((new PoiDetailSearchOption())
                .poiUid(uid));
    }





    @Override
    public void onGetPoiResult(PoiResult result) {
        LogUtils.LOGE("搜索附近返回");

        if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
         /*   Toast.makeText(context, "未找到更多的附近信息", Toast.LENGTH_LONG)
                    .show();*/
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(false);
            }
            return;
        }
        if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.LOGE("找到结果");
            poiResult=result;
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(true);
            }

            return;
        }
        if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
            if(mCallBackInterface!=null){
                mCallBackInterface.callBackFunction(false);
            }
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
        if (poiDetailResult.error != SearchResult.ERRORNO.NO_ERROR) {
            LogUtils.LOGE("抱歉，未找到结果");
            if(mDetailCallBack!=null){
                mDetailCallBack.callBackFunction(false);
            }
        } else {
          if(mDetailCallBack!=null){
              poiDetailResult=poiDetailResult;
              mDetailCallBack.callBackFunction(true);
          }
        }
    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

        LogUtils.LOGE("搜索返回结果onGetPoiIndoorResult");
    }





    public interface HttpInterface {
        public void callBackFunction(boolean httpClient, String result);

    }

    public void getLatLngLocation(LatLng latLng,final HttpInterface httpInterface){

        //&location=31.483686,120.315724
        String baseURL="http://api.map.baidu.com/geocoder/v2/?ak=rnFVwMpQZPV5521xNrQxHxGW&callback=renderReverse&output=json&pois=1&mcode=D2:7E:58:C0:3B:21:50:E7:F9:94:18:E2:15:41:4F:7A:FC:30:99:AE;com.example.car";
        baseURL=baseURL+"&location="+latLng.latitude+","+latLng.longitude;
        //LogUtils.LOGE("HttpPath:" + baseURL);

        OkHttpUtils.get().url(baseURL).tag("").params(null).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e) {
                LogUtils.LOGE("onError");
                String msg = "网络请求异常";
                call.cancel();
                ToastUtils.toastMsg(msg);
                httpInterface.callBackFunction(false, msg);
            }

            @Override
            public void onResponse(String response) {
                LogUtils.LOGE("onResponse");
                String result=response;
                result=result.substring(result.indexOf("(")+1,result.lastIndexOf(")"));
                LogUtils.LOGE("返回", "内容为:"+result);
                httpInterface.callBackFunction(true, result);

            }
        });




    }








    public void onDestroy() {
        mPoiSearch.destroy();
    }

}